package com.example.demo.services;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bouncycastle.asn1.eac.RSAPublicKey;
import org.slf4j.LoggerFactory;
import org.thymeleaf.util.DateUtils;

import java.security.interfaces.RSAPrivateKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;

public class JwtServer {

//    private static final Logger logger = LoggerFactory.getLogger(JWTKit.class);
    private static final String SECRET = "rds2#!G9Fds%^&Gg4>aV0@s]E56*Gh^3<Ud8(Rf3}Mxs1$mq5~d7JClR";
//    private static final  privateKey = "sadisah_)+@x12U*&xaxiash1y28y98xabxat^&TA&^F1sxdasxad1dxa";
    private static final String PAYLOAD = "payload";
    public static final Integer MAXAGE = 525600; //默认有效期为1年  单位/分钟
//

    public <T> String createToken(String Object) throws Exception {
        try {
            // 设置过期时间
            Date expiresDate = new Date(System.currentTimeMillis() + MAXAGE);
            // 设置密钥
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            // 加密
            String token = JWT.create()
                    .withIssuer("auth0")
                    .withClaim("Data", Object)
                    .withExpiresAt(expiresDate)
                    .sign(algorithm);

            return token;
        } catch (JWTCreationException exception){
            throw new Exception("创建令牌失败");
        }
    }

    public DecodedJWT decodeSign(String token) throws Exception
    {
        //String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt;
        } catch (JWTDecodeException exception){
            throw new Exception("解析令牌失败");
        }
    }

    /**
     * 签名每次访问放到header中
     * request.getHeader("Authorization")
     * @param object
     * @param maxAge (Minutes)
     * @return
     */
//    public <T> String sign(T object, Integer maxAge){
//        try {
//            if (object != null) {
//                if (maxAge == null) {
//                    maxAge = MAXAGE;
//                }
//
//                Map<String, Object> headerClaims = new HashMap<String, Object>();
//                headerClaims.put("alg", "HS256");
//                headerClaims.put("type", "JWT");
//                ObjectMapper mapper = new ObjectMapper();
//                String jsonString = mapper.writeValueAsString(object);
//                String token = JWT.create()
//                        .withHeader(headerClaims)
//                        .withClaim(PAYLOAD, jsonString)// payload
//                        .withExpiresAt(DateUtils.a(new Date(), maxAge))
//                        .sign(Algorithm.HMAC256(SECRET));// 加密
//
//                return token;
//            }
//
//        } catch (Exception e) {
//
//        }
//
//        return "";
//    }
}
